﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace HYRElectric.EquipmentPlace
{
	public partial class dlgMoveSwitch : System.Windows.Forms.Form
	{
		public double Distance
		{
			get
			{
				return this.m_dDist;
			}
			set
			{
				this.m_dDist = value;
			}
		}

		public OperateType operateType
		{
			get
			{
				if (this.rbBySelect.Checked)
				{
					return OperateType.OT_Select;
				}
				if (this.rbByCurrentView.Checked)
				{
					return OperateType.OT_CurrentView;
				}
				return OperateType.OT_Document;
			}
			set
			{
				if (value == OperateType.OT_Select)
				{
					this.rbBySelect.Checked = true;
					this.rbByCurrentView.Checked = false;
					this.rbByDocument.Checked = false;
					return;
				}
				if (value == OperateType.OT_CurrentView)
				{
					this.rbBySelect.Checked = false;
					this.rbByCurrentView.Checked = true;
					this.rbByDocument.Checked = false;
					return;
				}
				this.rbBySelect.Checked = false;
				this.rbByCurrentView.Checked = false;
				this.rbByDocument.Checked = true;
			}
		}

		public dlgMoveSwitch()
		{
			this.InitializeComponent();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.tbDist.Text == "")
			{
				MessageBox.Show("距门边不能设置为空，请检查！", "盈建科提示");
				this.Distance = 0.0;
				this.tbDist.Text = this.Distance.ToString("f0");
				return;
			}
			if (Convert.ToDouble(this.tbDist.Text) > 2000.0)
			{
				MessageBox.Show("距门边距离范围0~2000mm", "盈建科提示");
				this.Distance = 2000.0;
				this.tbDist.Text = this.Distance.ToString("f0");
				return;
			}
			this.Distance = Convert.ToDouble(this.tbDist.Text);
			base.DialogResult = DialogResult.OK;
		}

		private void tbDist_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void dlgMoveSwitch_Load(object sender, EventArgs e)
		{
			this.LoadDlgData();
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(new string[]
			{
				workingLocation + "dlgMoveSwitch.xml"
			});
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("tbDist", this.tbDist.Text);
			xmlHelper.SetValue("moveType", ((int)this.operateType).ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string text = Path.Combine(new string[]
			{
				workingLocation + "dlgMoveSwitch.xml"
			});
			if (!File.Exists(text))
			{
				return;
			}
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			this.tbDist.Text = xmlHelper.GetValue("tbDist");
			int operateType = Convert.ToInt32(xmlHelper.GetValueEx("moveType", "1"));
			this.operateType = (OperateType)operateType;
		}

		private void dlgMoveSwitch_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveDlgData();
		}

		private double m_dDist;
	}
}
